package ar.com.fiuba.domain.api;

import java.util.ArrayList;

import ar.com.fiuba.domain.CoeficientCalculationStrategy;
import ar.com.fiuba.domain.Dictionary;
import ar.com.fiuba.domain.Word;
import ar.com.fiuba.domain.exceptions.DomainException;

public class Corrector {
	
	Dictionary dictionary;
	CoeficientCalculationStrategy coefficientCalculator;
	
	public Corrector(Dictionary dictionary, CoeficientCalculationStrategy coefficientCalculator)
	{
		this.dictionary = dictionary;
		this.coefficientCalculator = coefficientCalculator;
	}
	
	public ArrayList<String> getWordsWithSameNGrams(String wordToCorrect)
	{
		ArrayList<String> wordsWithSameNgrams = dictionary.getWordsWithSameNGramms(wordToCorrect);
		return wordsWithSameNgrams;
	}
	
	public String getBestMatch(String wordToCorrect)
	{
		String currentWord ="";
		double maxCoefficient = 0;
		
		for (String s:getWordsWithSameNGrams(wordToCorrect))
		{
			double coef = 0;
			try {
				coef = coefficientCalculator.calculate(new Word(wordToCorrect,  dictionary.getNgramLength()), s, dictionary.getNgramLength());
			} catch (DomainException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(coef >= maxCoefficient){	
					maxCoefficient = coef; 
					currentWord = s;
			}		
		}
		return currentWord;
	}
	
	public String getDictionaryFileName()
	{
		return this.dictionary.getFilename();
	}

}
